package com.qiguliuxing.dts.wx.web;

import com.alibaba.fastjson.JSONObject;
import com.qiguliuxing.dts.core.util.ResponseUtil;
import com.qiguliuxing.dts.db.domain.DtsCategory;
import com.qiguliuxing.dts.db.service.DtsCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 简单分类服务
 */
@RestController
@RequestMapping("/wx/cate")
@Validated
public class WxCateController {
    private static final Logger logger = LoggerFactory.getLogger(WxCateController.class);

    @Autowired
    private DtsCategoryService categoryService;

    /**
     * 分类详情
     *
     * @param id 分类类目ID。 如果分类类目ID是空，则选择第一个分类类目。
     * @return 分类详情
     */
    @GetMapping("index")
    public Object index(Integer id) {
        logger.info("【请求开始】分类详情,请求参数,id:{}", id);

        // 所有一级分类目录
        List<DtsCategory> l1CatList = categoryService.queryL1();

        // 当前一级分类目录
        DtsCategory currentCategory = null;
        if (id != null) {
            currentCategory = categoryService.findById(id);
        } else {
            currentCategory = l1CatList.get(0);
        }
        Map<String, Object> data = new ConcurrentHashMap<>();
        //所有分类
        data.put("categoryList", l1CatList);
        //当前分类
        data.put("currentCategory", currentCategory);

        logger.info("【请求结束】分类详情,响应结果：{}", JSONObject.toJSONString(data));
        return ResponseUtil.ok(data);
    }


}
